package com.bblei.hbaseDemo.mapReduceDemo;

import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;

import java.io.IOException;

import static com.bblei.hbaseDemo.mapReduceDemo.HBaseMr.col;
import static com.bblei.hbaseDemo.mapReduceDemo.HBaseMr.colf;

public class MyReducer extends TableReducer<Text, IntWritable, ImmutableBytesWritable>{

    protected void reduce(Text key, Iterable<IntWritable> values,
                          Context context) throws IOException, InterruptedException {

        //对mapper的数据求和
        int sum = 0;
        for (IntWritable val : values) {//叠加
            sum += val.get();
        }
        // 创建put，设置rowkey为单词
        Put put = new Put(Bytes.toBytes(key.toString()));
        // 封装数据
        put.add(Bytes.toBytes(colf), Bytes.toBytes(col),Bytes.toBytes(String.valueOf(sum)));
        //写到hbase,需要指定rowkey、put
        context.write(new ImmutableBytesWritable(Bytes.toBytes(key.toString())),put);
    }

}
